package cn.cmcc.intertelecom.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.cmcc.intertelecom.bean.Dictionary;
import cn.cmcc.intertelecom.bean.DictionaryType;
import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.service.DictionaryService;
import cn.cmcc.intertelecom.utils.ArchivesLog;
@Controller
public class DictionaryController {
	@Autowired
	private DictionaryService dictionaryService;
	
	
	/**
	 * 批量删除一批数据 或者删除一条记录
	 * @param dContentIds
	 * @return
	 */
	@ArchivesLog(operationPower="delete_generator_room")
	@ResponseBody
	@RequestMapping(value="/dContent/{dContentIds}" ,method=RequestMethod.DELETE)
	public Msg deleteUser(@PathVariable(value="dContentIds")String dContentIds){

		if(dContentIds.contains("-")){
			String[] str = dContentIds.split("-");
			List<Integer> ids=new ArrayList<>();
			for (String string : str) {
				ids.add(Integer.parseInt(string));
			}

			dictionaryService.deleteBatch(ids);
		}else{
			dictionaryService.deleteDept(Integer.parseInt(dContentIds));
		}
		return Msg.success();
	}
	
	/**
	 * 新建字典内容
	 * @param dictionary
	 * @return
	 */
	@ArchivesLog(operationPower="new_generator_room")
	@ResponseBody
	@RequestMapping(value="/dContent",method=RequestMethod.POST)
	public Msg saveDContent(Dictionary dictionary){
		
		dictionaryService.insertDContent(dictionary);
		
		return Msg.success();
	}
	
	 /**
	  * 更新字典内容
	  * @param dictionary
	  * @return
	  */
	@ArchivesLog(operationPower="update_generator_room")
	@ResponseBody
	@RequestMapping(value="/dContent",method=RequestMethod.PUT)
	public Msg updateDContent(Dictionary dictionary){
		
		dictionaryService.updateById(dictionary);
		
		return Msg.success();
	}
	
	/**
	 * 根据字典的id获取字典内容
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dContent/{id}",method=RequestMethod.GET)
	public Msg getDContentById(@PathVariable(value="id")Integer id){
		
		Dictionary dictionary=dictionaryService.selectById(id);
		
		return Msg.success().add("dContent", dictionary);
	}
	
	/**
	 * 根据 已知的一些信息  查询字典  包括其类型
	 * @param pn
	 * @return
	 */
	@ArchivesLog(operationPower="query_generator_room")
	@ResponseBody
	@RequestMapping(value="/dContentes/{pn}",method=RequestMethod.GET)
	public Msg queryDContent(@PathVariable(value="pn")Integer pn,Dictionary dictionary){
		PageHelper.startPage(pn,5);
		List<Dictionary> list=dictionaryService.selectAllWithType(dictionary);
		PageInfo<Dictionary> pageInfo=new PageInfo<>(list);
		return Msg.success().add("pageInfo", pageInfo);
	}
	
	/**
	 * 根据分页 查询字典 包括其类型
	 * @param pn
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/dContentes",method=RequestMethod.GET)
	public Msg getDContent(@RequestParam(value="pn",defaultValue="1")Integer pn){
		PageHelper.startPage(pn,5);
		List<Dictionary> list=dictionaryService.selectAllWithType();
		PageInfo<Dictionary> pageInfo=new PageInfo<>(list);
		return Msg.success().add("pageInfo", pageInfo);
	}
}
